package top.lucky.utils;

import java.nio.charset.StandardCharsets;
import java.util.Base64;

public class Base64Utils {
    private Base64Utils() {
    }

    public static byte[] encode(byte[] src) {
        return Base64.getEncoder().encode(src);
    }

    public static byte[] encode(String src) {
        return encode(src.getBytes(StandardCharsets.UTF_8));
    }

    public static String encodeToString(byte[] src) {
        return Base64.getEncoder().encodeToString(src);
    }

    public static String encodeToString(String src) {
        return encodeToString(src.getBytes(StandardCharsets.UTF_8));
    }

    public static byte[] decode(byte[] encrypt) {
        return org.apache.commons.codec.binary.Base64.decodeBase64(encrypt);
    }

    public static byte[] decode(String encrypt) {
        return decode(encrypt.getBytes(StandardCharsets.UTF_8));
    }

    public static String decodeToString(byte[] encrypt) {
        return new String(decode(encrypt), StandardCharsets.UTF_8);
    }

    public static String decodeToString(String encrypt) {
        return new String(decode(encrypt.getBytes(StandardCharsets.UTF_8)), StandardCharsets.UTF_8);
    }

    public static byte[] urlEncode(byte[] src) {
        return Base64.getUrlEncoder().encode(src);
    }

    public static byte[] urlEncode(String src) {
        return Base64.getUrlEncoder().encode(src.getBytes(StandardCharsets.UTF_8));
    }

    public static String urlEncodeToString(byte[] src) {
        return Base64.getUrlEncoder().encodeToString(src);
    }

    public static String urlEncodeToString(String src) {
        return Base64.getUrlEncoder().encodeToString(src.getBytes(StandardCharsets.UTF_8));
    }

    public static byte[] urlDecode(byte[] encrypt) {
        return (new org.apache.commons.codec.binary.Base64(true)).decode(encrypt);
    }

    public static byte[] urlDecode(String encrypt) {
        return urlDecode(encrypt.getBytes(StandardCharsets.UTF_8));
    }

    public static String urlDecodeToString(byte[] encrypt) {
        return new String(urlDecode(encrypt), StandardCharsets.UTF_8);
    }

    public static String urlDecodeToString(String encrypt) {
        return new String(urlDecode(encrypt.getBytes(StandardCharsets.UTF_8)), StandardCharsets.UTF_8);
    }
}
